In the field of computer software, an “event” is an action or occurrence in a computer system to which a computer program might respond. For example, an event may be a graphical user interface (GUI) event, such as key presses, mouse movement, or action selections. An event may also be the occurrence of a timer expiring. On a lower level, events can represent availability of new data for reading a file or network stream. The events are created by the operating system or run-time environment based on interpreting lower-level inputs, which may be lower-level events themselves. For example, mouse movements and clicks are interpreted as menu selections. The events may initially originate from actions on the operating system level, such as interrupts generated by hardware devices, software interrupt instructions, or state changes in polling. An event handler is a function written to handle one or more events as they occur during execution of a program.
Conventionally, event handling is often performed through polling, by a program, to check the state of an event queue to determine if a new event has been enqueued. The constant polling of the event queue causes continual disruption in the execution of the program. This problem is exacerbated when, in many cases, the events rarely occur. Programs often need to listen for these rare events and to handle them. When the program is ready to handle the events, it checks the state of the event queue and performs appropriate actions. The actions may depend on the current state of the program. For example, a program might poll for timer tick events, which occur when a specified timer interval has elapsed, to pass control to a thread scheduler. The events are rare, but the code for event handling needs to be generated and loaded into memory regardless of their rarity. Not only does the program become slower and its code become bloated, but the compiler also has to know that event handling code is very rarely executed. Otherwise the code generation and register allocation for frequently executed code becomes suboptimal.
The problem is exacerbated even further when programs are executed on memory-constrained devices, for example, portable wireless devices. One major limitation of these wireless devices is the relatively small amount of memory available for executing programs. The extra code needed for handling rare events bogs down the already-limited memory of the memory-constrained device. Accordingly, there is a need to optimize rare event handling in program code executing on memory-constrained devices.